﻿namespace instamuz.DataAccess.Tests
{
    using System;
    using System.Data.Common;
    using NHibernate.Cfg;
    using NHibernate.Engine;
    using NHibernate.Tool.hbm2ddl;
    using Xunit;
    using instamuz.DataAccess;

    internal class LiveDatabaseTests
    {
        [Fact]
        public void GenerateCreationScript()
        {
            new SchemaExport(Configuration)
                .SetDelimiter("\r\nGO\r\n")
                .Execute(true, false, false);
        }

        [Fact]
        public void GenerateMigrationScript()
        {
            var cfg = Configuration;
            var factory = (ISessionFactoryImplementor) cfg.BuildSessionFactory();

            using (var session = factory.OpenSession())
            {
                var updateScripts = cfg.GenerateSchemaUpdateScript(factory.Dialect,
                                                                   new DatabaseMetadata(
                                                                       (DbConnection) session.Connection,
                                                                       factory.Dialect));

                foreach (var updateScript in updateScripts)
                {
                    Console.WriteLine(updateScript);
                    Console.WriteLine(@"GO");
                }
            }
        }

        private Configuration Configuration
        {
            get { return new NHibernateInitializer().GetConfiguration(); }
        }
    }
}